package regex;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 黄乾坤
 * @create 2022-11-09 13:50
 */
@Slf4j
public class Test {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        String s =
                "<p><img src=\"https://ss1-pre.citictrust.com.cn/cit/mis/view/20221109/d6230417a42543df902422e09394fcf6.png\" alt=\"\" width=\"200\" height=\"113\">暴富</p>\n" +
                        "<p style=\"margin: 2em 0px; padding: 0px; font: 400 18px / 2 微软雅黑; color: rgb(15, 15, 15); letter-spacing: normal; orphans: 2; text-transform: none; " +
                        "white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: " +
                        "initial; text-decoration-style: initial; text-decoration-color: initial; text-indent: 2em; text-align: justify;" +
                        "\">二十大代表谢兰是重庆两江新区人和街道邢家桥社区党委书记。眼下，谢兰所在的社区正在进行老旧小区改造，在跟大伙学习宣传二十大报告时，她结合老旧小区改造谈了自己的感受和体会。</p>\n" + "<p style=\"margin: 2em 0px; padding: 0px; font: 400 " +
                        "18px / 2 微软雅黑; color: rgb(15, 15, 15); letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; " +
                        "-webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; " +
                        "text-decoration-color: initial; text-indent: 2em; text-align: justify;\">&nbsp;</p>\n" + "<p style=\"margin: 1em 0px; padding: 0px; font: 400 18px / 2em" +
                        " 微软雅黑; color: rgb(15, 15, 15); letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing:" +
                        " 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; " +
                        "text-decoration-color: initial; text-align: center;\"><img style=\"vertical-align: top; border: none; max-width: 640px;\" src=\"\" data-origin-src=\"/\"" +
                        " data-mce-attachid=\"3500009\" data-mce-alias=\"3288132059.jpg\"></p>\n" + "<p style=\"margin: 2em 0px; padding: 0px; font: 400 18px / 2 微软雅黑; color: " +
                        "rgb(15, 15, 15); letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width:" +
                        " 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; " +
                        "text-indent: 2em; text-align: justify;\">邢家桥社区老旧小区改造一期工程已经完工，二期工程也进入尾声。目前，大部分设施陆续投入使用，极大方便了居民的生活</p>";

        String s1 = convertTmpImg(s);
        System.out.println(s1);
    }


    private static String convertTmpImg(String content){
        log.info("content: " + content);
        String result = content;
        final Pattern imgPattern = PatternPool.get("(?i)<img(\\s+[^>]*?)?/?>(.*?</img>)?", Pattern.DOTALL);
        final Pattern srcPattern = PatternPool.get("(?i) src\\s*=\\s*\"?(.*?)(\"|>|\\s+)", Pattern.DOTALL);
        Matcher matcher = imgPattern.matcher(content);
        while (matcher.find()) {
            String oldImg = matcher.group();
            log.info("oldImg: " + oldImg);
            String newImg = null;
            Matcher srcMatcher = srcPattern.matcher(oldImg);
            if(srcMatcher.find()){
                String oldSrc = srcMatcher.group(1);
                log.info("oldSrc: " + oldSrc);
                String newSrc = null;
                if(HtmlUtil.unescape(oldSrc).startsWith("https://wx-dev.citictrust.com.cn:443/file/anonymous/basic/manage?fr=TMP&path=")){
                    Map<String, String> paramMap = HttpUtil.decodeParamMap(oldSrc, StandardCharsets.UTF_8);
                    if(paramMap.get("fr") != null && paramMap.get("path") != null){
                        newSrc = new StringBuilder("https://ss1.citictrust.com.cn/cit/").append(paramMap.get("path")).toString();
                    }
                }
                log.info("newSrc: " + newSrc);
                newImg = oldImg.replace(oldSrc, HtmlUtil.escape(newSrc));
                log.info("newImg: " + newImg);
                result = result.replace(oldImg, newImg);
                log.info("result: " + result);
            }
        }
        return result;
    }
}
